package com.yilin.medical.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/****************************
 * 创建时间：2014-6-12 上午10:06:58  
 * 项目名称：JxZhagen  
 * @author 赵强
 * @version 1.0   
 * @since JDK 1.7.0_15
 * 文件名称：JxNetWork.java  
 * 类说明：  
 ****************************/

public class JxNetWork {
	public static boolean isNetworkAvailable(Context context) {

		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connManager.getActiveNetworkInfo();

		if (info != null && info.isConnectedOrConnecting()) {

			return true;
		}

		return false;
	}


    public static final  int NETWORK_NOT = 0;
    public static final  int NETWORK_WIFI = 1;
    public static final  int NETWORK_2G = 2;
    public static final  int NETWORK_3G = 3;

    /**
     * 判断当前是否有可用的网络以及网络类型 0：无网络 1：WIFI 2：2G 3：3G
     *
     * @param context
     * @return
     */
    private static  int  getNetworkAvailableCode(Context context) {
        if (context==null) {
            return NETWORK_NOT;
        }
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return NETWORK_NOT;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        NetworkInfo netWorkInfo = info[i];
                        if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            return NETWORK_WIFI;
                        } else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
//							String extraInfo = netWorkInfo.getExtraInfo();
//							if ("cmwap".equalsIgnoreCase(extraInfo)
//									|| "cmwap:gsm".equalsIgnoreCase(extraInfo)) {
//								return 2;
//							}
                            if(netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
                                    || netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA
                                    || netWorkInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE){
                                return NETWORK_2G;
                            }
                            return NETWORK_3G;
                        }
                    }
                }
            }
        }
        return 0;
    }

    public static boolean  hasWifi(Context context){
        return getNetworkAvailableCode(context) == NETWORK_WIFI;
    }

    public static boolean  has3G(Context context){
        return getNetworkAvailableCode(context) == NETWORK_3G;
    }

    public static boolean  has2G(Context context){
        return getNetworkAvailableCode(context) == NETWORK_2G;
    }

    public static boolean  hasNetWork(Context context){
        return getNetworkAvailableCode(context) != NETWORK_NOT;
    }



}
  